package datastructure.list;

/**
 * DESCRIPTION:
 * Author: ammar
 * Date:   2021/12/9
 * Time:   上午9:04
 */
public class ArrayList<E> extends AbstractList<E> {
    // 数组初始长度 100
    private static final int DEFAULT_LENGTH = 100;
    // 数组总容量
    private static int CAPACITY = 0;
    private E[] elements;

    public ArrayList() {
        this(DEFAULT_LENGTH);
    }
    public ArrayList(int length) {
        elements = (E[]) new Object[length];
        size = 0;
        CAPACITY = length;
    }

    public void clear() {
        size = 0;
        elements = (E[]) new Object[DEFAULT_LENGTH];
    }

    /**
     * 在 index 位置插入一个元素
     *
     * @param index
     * @param e
     */
    public void add(int index, E e) {
        rangeCheckAdd(index);
        ensureCapacity(size + 1);
        for (int i = size; i >= index; i--) {
            if (i == index) {
                elements[index] = e;
            } else {
                elements[i] = elements[i - 1];
            }
        }
        size++;
    }

    /**
     * 获取 index 位置的元素
     *
     * @param index
     * @return
     */
    public E get(int index) {
        rangeCheck(index);
        return elements[index];
    }

    /**
     * 设置 index 位置的元素
     *
     * @param index
     * @param e
     */
    public void set(int index, E e) {
        rangeCheck(index);
        elements[index] = e;
    }

    /**
     * 删除 index 位置的元素
     *
     * @return 返回删除的元素
     */
    public E remove(int index) {
        rangeCheck(index);
        shrinkCapacity();
        E element = elements[index];
        for (int i = index; i < size; i++) {
            if (i < size - 1) {
                elements[i] = elements[i + 1];
            }
        }
        elements[size - 1] = null;
        size--;
        return element;
    }

    /**
     * 查看元素的索引
     *
     * @param e
     * @return
     */
    public int indexOf(E e) {
        for (int i = 0; i < size; i++) {
            if (elements[i].equals(e)) {
                return i;
            }
        }
        return ELEMENT_NOT_FOUND;
    }

    /**
     * 确保容量可以包容 index
     *
     * @param newCapacity 容量大小
     */
    private void ensureCapacity(int newCapacity) {
        if (newCapacity < CAPACITY) return;
        // 扩容
        CAPACITY = (int) (CAPACITY * 1.5);
        E[] newElements = (E[]) new Object[CAPACITY];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("数组扩容至" + CAPACITY);
    }

    /**
     * 缩容
     */
    private void shrinkCapacity() {
        if (CAPACITY <= DEFAULT_LENGTH) return;
        if (CAPACITY * 0.5 - 1 < size) return;
        // 缩容
        CAPACITY = (int) (CAPACITY * 0.5);
        E[] newElements = (E[]) new Object[CAPACITY];
        for (int i = 0; i < size; i++) {
            newElements[i] = elements[i];
        }
        elements = newElements;
        System.out.println("数组缩容至" + CAPACITY);
    }

    @Override
    public String toString() {
        // size=3, [99, 88, 77]
        StringBuilder string = new StringBuilder();
        string.append("size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            if (i != 0) {
                string.append(", ");
            }

            string.append(elements[i]);

//			if (i != size - 1) {
//				string.append(", ");
//			}
        }
        string.append("]");
        return string.toString();
    }
}
